*final update 2025.02.03



********************************************************************************
//STEP 01: CREATE LOCALS
********************************************************************************
//Declare dataset a panel
tsset 	id_city id_time

*Directory to the Alpha_Do_File .do file's location
local 	home_folder "/Users/demattee/Desktop/JLC_Replication"

local MA		"5"					// What is MA's id_city? UNSURE: tab id_city if hearing_base_city=="BOS", nolab
local units		"24"				// How many jurisdictions are there?

//IDENTIFY THE STATING DATES
local begin		"1"					// The first id_time in the analysis
local MSPEstart "1"					// Period in which MSPE minimization begins 														
		
//IDENTIFY THE TREATMENT DATES
local pretreat	"38"				// What id_time corresponds with the Brito vs. Barr decision date
local decision	"39"				// Brito vs. Barr opinion published (Wednesday, Nov 27, 2019).
										// id_time	time	date_day
										// 32		201948	2019.11.27
									// court orders that immigration courts shall follow the requirements, effective Friday, December 13, 2019.
										// id_time	time	date_day
										// 34		201950	2019.12.13
local posttreat "40"				// What is the first id_time after the decision date? UNSURE: decision +1

//IDENTIFY THE ENDING DATES
local endtime	"52"				// id_time 53==2020 week 12 (Friday, March 20th) as the last id_time in the analysis?
local covid		"52"				// POTUS Declares COVID-19 a National Emergency (Friday, March 13).
										
//Set locals to ensure that all analyses run the same model
local outcome 	"granted_bond"													 
local pretime 	"granted_bond(13) granted_bond(17) granted_bond(20) granted_bond(38)"
local court		"trump_ij obama_ij ij_experience"								
local case		"crime_charge_yes meeting_number"								
local econ		"log_state_gdp"													
local society	"log_state_pop state_govt_employees_capita"	


********************************************************************************
// STEP 02: RUN PLACEBO IN SPACE AND GENERATE TREATMENT EFFECT GRAPHS
********************************************************************************

*Tell Stata where to save these .dta files it generates
cd "`home_folder'/Data/Placebo Results Granted Bond/Weekly"	

//use loops to run synth treating each unit in the sample as the control
tempname resmat
foreach i of numlist 1/`units' {	 
	synth 	`outcome' 																///
			`pretime' `court' `case' `politics' `econ' `society'					///
			, trunit(`i') trperiod(`decision') 										///
			mspeperiod(`MSPEstart'(1)`pretreat')									/// Results obtained for periods. This is how we change the years displayed in the figure
			nested /// allopt   													/// 
			keep(synth_`i') replace
	matrix `resmat' = nullmat(`resmat') \ e(RMSPE)
	local names `"`names' `"`i'"'"'
	}
		mat colnames `resmat' = "RMSPE"
		mat rownames `resmat' = `names'
		matlist `resmat' , row("Treated Unit")
	
	
//use loops and save data to to estimate treate/synth difference for each placebo
foreach i of numlist 1/`units' {	 
	use synth_`i', clear
	gen 	diff_`i'	=_Y_treated - _Y_synthetic

	gen 	e2= diff_`i'^2
	gen		pretreat  = 1 if _time < `decision'
	gen 	posttreat = 1 if _time > `decision'	
	rename 	_time week
	keep 	week diff_`i'
	drop if week==.
	save 	synth_`i', replace
	}

	
//Merge and save placebo results	
use synth_`MA', clear
foreach i of numlist 1/`units' {	 
	qui merge 1:1 week using synth_`i', nogenerate
}
save synth_placebo_bond_weekly_data, replace



********************************************************************************	
// 03: GRAPH PLACEBO RESULTS AND SAVE THEM
********************************************************************************
*Tell Stata where to save these .dta files it generates
cd "`home_folder'/Data/Placebo Results Granted Bond/Weekly"	

//what does this graph show?
local lp
foreach i of numlist 1/`units' {	 
   local lp `lp' line diff_`i' week, lcolor(gs12) ||
}

	label define 	week_labels	1	"19-Mar"	///	2019.03.19
								2	"28-Mar"	///	2019.03.28
								3	"2-Apr"		///	2019.04.02
								4	"10-Apr"	///	2019.04.10
								5	"17-Apr"	///	2019.04.17
								6	"23-Apr"	///	2019.04.23
								7	"30-Apr"	///	2019.04.30
								8	"13-May"	///	2019.05.13
								9	"16-May"	///	2019.05.16
								10	"21-May"	///	2019.05.21
								11	"30-May"	///	2019.05.30
								12	"10-Jun"	///	2019.06.10
								13	"13-Jun"	///	2019.06.13
								14	"24-Jun"	///	2019.06.24
								15	"25-Jun"	///	2019.06.25
								16	"3-Jul"		///	2019.07.03
								17	"9-Jul"		///	2019.07.09
								18	"18-Jul"	///	2019.07.18
								19	"24-Jul"	///	2019.07.24
								20	"30-Jul"	///	2019.07.30
								21	"8-Aug"		///	2019.08.08
								22	"19-Aug"	///	2019.08.19
								23	"20-Aug"	///	2019.08.20
								24	"27-Aug"	///	2019.08.27
								25	"4-Sep"		///	2019.09.04
								26	"10-Sep"	///	2019.09.10
								27	"17-Sep"	///	2019.09.17
								28	"25-Sep"	///	2019.09.25
								29	"1-Oct"		///	2019.10.01
								30	"10-Oct"	///	2019.10.10
								31	"15-Oct"	///	2019.10.15
								32	"23-Oct"	///	2019.10.23
								33	"31-Oct"	///	2019.10.31
								34	"5-Nov"		///	2019.11.05
								35	"13-Nov"	///	2019.11.13
								36	"19-Nov"	///	2019.11.19
								37	"27-Nov"	///	2019.11.27
								38	"5-Dec"		///	2019.12.05
								39	"10-Dec"	///	2019.12.10
								40	"23-Dec"	///	2019.12.23
								41	"30-Dec"	///	2019.12.30
								42	"2-Jan"		///	2020.01.02
								43	"9-Jan"		///	2020.01.09
								44	"16-Jan"	///	2020.01.16
								45	"23-Jan"	///	2020.01.23
								46	"29-Jan"	///	2020.01.29
								47	"10-Feb"	///	2020.02.10
								48	"12-Feb"	///	2020.02.12
								49	"20-Feb"	///	2020.02.20
								50	"26-Feb"	///	2020.02.26
								51	"5-Mar"		///	2020.03.05
								52	"11-Mar"	///	2020.03.11
								, replace
	label values week week_labels



********************************************************************************	
// SETP 04: PLOT THE TREATED VS. SYNTH DIFFERENCES
********************************************************************************
//Graph the Boston Plot
twoway `lp' || line diff_`MA' week, 												///
			xline(`decision', lpattern(dash)) xlabel(`begin'(4)`endtime') 			///
			ytitle(Treatment Effect on Rate Granted Bond, size(small)) 				///
			xtitle("") title("Treatment Group", size(small)) 						///
			graphregion(c(gs16)) 													///
			plotregion(ic(gs16)) 													///
			legend(off)

//now save the graphing data
save synth_placebo_bond_weekly_graph, replace

	
//IDENTIFY THE TREATMENT UNIT
********************************************************************************
********************************************************************************
local MA		"5"					// What is MA's id_city? UNSURE: tab id_city if hearing_base_city=="BOS", nolab
local units		"24"				// How many jurisdictions are there?

//IDENTIFY THE STATING DATES
local begin		"1"					// The first id_time in the analysis
local MSPEstart "1"					// Period in which MSPE minimization begins 														
		
//IDENTIFY THE TREATMENT DATES
local pretreat	"38"				// What id_time corresponds with the Brito vs. Barr decision date
local decision	"39"				// Brito vs. Barr opinion published (Wednesday, Nov 27, 2019).
										// id_time	time	date_day
										// 32		201948	2019.11.27
									// court orders that immigration courts shall follow the requirements, effective Friday, December 13, 2019.
										// id_time	time	date_day
										// 34		201950	2019.12.13
local posttreat "40"				// What is the first id_time after the decision date? UNSURE: decision +1

//IDENTIFY THE ENDING DATES
local endtime	"52"				// id_time 53==2020 week 12 (Friday, March 20th) as the last id_time in the analysis?
local covid		"52"				// POTUS Declares COVID-19 a National Emergency (Friday, March 13).
										
//Set locals to ensure that all analyses run the same model
local outcome 	"granted_bond"													 
local pretime 	"granted_bond(13) granted_bond(17) granted_bond(20) granted_bond(38)"
local court		"trump_ij obama_ij ij_experience"								
local case		"crime_charge_yes meeting_number"								
local econ		"log_state_gdp"													
local society	"log_state_pop state_govt_employees_capita"	

********************************************************************************
//Graph the the treatment effect in MA only
sort week
graph twoway	(line diff_`MA' week, clpattern(solid) lcolor(black)), 					///
				title("Rate Granted Bond Between Treatment Unit and Synthetic", size(small)) 	///
				subtitle("") caption("") note("$S_DATE", size(tiny) span)				///
				xtitle("") 																///			
					xline(`decision', lpattern(dash) lcolor(red))						///	
					xline(`covid', lwidth(medthick) lpattern(tight_dot) lcolor(black))	///
					xlabel(`begin'(4)`covid',  labsize(small) valuelabel) xmtick(##5) 	///
				ytitle(Treatment Effect on Rate Granted Bond, size(small)) 				///
					ylabel(-.6(.3).6, labsize(small) gmin gmax) ymtick(##3) 			///
					yline(0, lpattern(solid) lcolor(gs10))								///	
				graphregion(c(gs16)) 													///
				plotregion(ic(gs16)) 													///
				legend(on order(1 "Boston") 											///
						cols(1) size(vsmall) nobox region(fcolor(none) lcolor(none)) 	///
						position(11) ring(0))											//
				graph save "Graph" 	"`home_folder'/Graphs/bond_te_weekly.gph", replace	
				graph export 		"`home_folder'/Graphs/bond_te_weekly.jpg", as(jpg) name("Graph") quality(100) replace



********************************************************************************
//Graph All Possible Placebos 
graph twoway 	(line diff_1 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_2 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_3 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_4 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_6 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_7 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_8 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_9 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_10 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_11 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_12 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_13 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_14 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_15 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_16 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_17 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_18 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_19 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_20 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_21 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_22 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_23 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_24 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_5 week, clpattern(solid) lcolor(black))					///
				, 																	/// 
			title("Panel A: Granted Bond Rate", size(small)) 		///
			subtitle("Full Donor Pool", size(vsmall)) caption("") note("$S_DATE", size(tiny) span)				///
			xtitle("") 																///			
				xline(`decision', lpattern(dash) lcolor(red))						///	
				xline(`covid', lwidth(medthick) lpattern(tight_dot) lcolor(black))	///
				xlabel(`begin'(4)`covid',  labsize(small) valuelabel) xmtick(##6) 		///
			ytitle(Treatment Effect on Rate Granted Bond, size(small)) 				///
				ylabel(-.6(.3).6, labsize(small) grid gmin gmax) ymtick(##3) 		///
				yline(0, lpattern(solid) lcolor(gs10))								///	
			graphregion(c(gs16)) 													///
			plotregion(ic(gs16)) 													///
			legend(on order(24 "Boston" 1 "Placebo Tests") 							///
					cols(1) size(vsmall) nobox region(fcolor(none) lcolor(none)) 	///
					position(11) ring(0))											//
			graph save "Graph" 	"`home_folder'/Graphs/placebo_bond_weekly_full.gph", replace
			graph export 		"`home_folder'/Graphs/placebo_bond_weekly_full.jpg", as(jpg) name("Graph") quality(100) replace


********************************************************************************
//Graph Placebos Minus Those with Largest RMPSEs
graph twoway 	(line diff_1 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_2 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_3 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_4 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_6 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_7 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_8 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_9 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_10 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_11 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_12 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_13 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_14 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_15 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_16 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_17 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_18 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_19 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_20 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_21 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_22 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_23 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_24 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_5 week, clpattern(solid) lcolor(black))					///
				, 																	/// 
			title("Panel B: Granted Bond Rate", size(small)) 						///
			subtitle("Excludes Five Largest RMSPEs", size(vsmall)) caption("") note("$S_DATE", size(tiny) span)				///
			xtitle("") 																///			
				xline(`decision', lpattern(dash) lcolor(red))						///	
				xline(`covid', lwidth(medthick) lpattern(tight_dot) lcolor(black))	///
				xlabel(`begin'(4)`covid',  labsize(small) valuelabel) xmtick(##6) 		///
			ytitle(Treatment Effect on Rate Granted Bond, size(small)) 				///
				ylabel(-.6(.3).6, labsize(small) grid gmin gmax) ymtick(##3) 		///
				yline(0, lpattern(solid) lcolor(gs10))								///	
			graphregion(c(gs16)) 													///
			plotregion(ic(gs16)) 													///
			legend(on order(24 "Boston" 2 "Placebo Tests") 							///
					cols(1) size(vsmall) nobox region(fcolor(none) lcolor(none)) 	///
					position(11) ring(0))											//
			graph save "Graph" 	"`home_folder'/Graphs/placebo_bond_weekly_cut5.gph", replace
			graph export 		"`home_folder'/Graphs/placebo_bond_weekly_cut5.jpg", as(jpg) name("Graph") quality(100) replace

********************************************************************************
//Graph Placebos Minus Those with semi-Largest RMPSEs
graph twoway 	(line diff_1 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_2 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_3 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_4 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_6 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_7 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_8 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_9 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_10 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_11 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_12 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_13 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_14 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_15 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_16 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_17 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_18 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_19 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_20 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_21 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_22 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_23 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_24 week, clpattern(solid) lcolor(gs12) lwidth(thin)) 	///
				(line diff_5 week, clpattern(solid) lcolor(black))					///
				, 																	/// 
			title("Panel C: Granted Bond Rate", size(small)) 						///
			subtitle("Excludes Ten Largest RMSPEs", size(vsmall)) caption("") note("$S_DATE", size(tiny) span)				///
			xtitle("") 																///			
				xline(`decision', lpattern(dash) lcolor(red))						///	
				xline(`covid', lwidth(medthick) lpattern(tight_dot) lcolor(black))	///
				xlabel(`begin'(4)`covid',  labsize(small) valuelabel) xmtick(##6) 		///
			ytitle(Treatment Effect on Rate Granted Bond, size(small)) 				///
				ylabel(-.6(.3).6, labsize(small) grid gmin gmax) ymtick(##3) 		///
				yline(0, lpattern(solid) lcolor(gs10))								///	
			graphregion(c(gs16)) 													///
			plotregion(ic(gs16)) 													///
			legend(on order(24 "Boston" 2 "Placebo Tests") 							///
					cols(1) size(vsmall) nobox region(fcolor(none) lcolor(none)) 	///
					position(11) ring(0))											//
			graph save "Graph" 	"`home_folder'/Graphs/placebo_bond_weekly_cut10.gph", replace
			graph export 		"`home_folder'/Graphs/placebo_bond_weekly_cut10.jpg", as(jpg) name("Graph") quality(100) replace


			
********************************************************************************
// STEP 05: Return to JLC_01a_Synth_Granted_Bond_Weekly.do
********************************************************************************			
			
